/**
* ${table.comment}管理
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>${table.comment}</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="名称">
        <el-input size="small" v-model="formInline.title" placeholder="输入名称"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search${table.entityName}">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="toAdd${table.entityName}('add')">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table style="width: 100%" row-key="id" border lazy
              :row-style="{maxHeight: '94px'}"
              :data="listData">
      <#-- ----------  BEGIN 字段循环遍历  ---------->
      <#list table.fields as field>
        <el-table-column
                <#if field.keyFlag>
                  label="序号"
                <#else>
                  label="${field.comment}"
                </#if>
                <#if field.propertyType == "String">
                  show-overflow-tooltip
                </#if>
                prop="${field.propertyName}">
        </el-table-column>
      </#list>
      <el-table-column align="center" label="操作" min-width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="toAdd${table.entityName}('update',scope.row)">编辑</el-button>
          <el-button size="mini" type="warning" v-if="scope.row.status == 1"
                     @click="unable${table.entityName}( scope.row)">停用
          </el-button>
          <el-button size="mini" type="success" v-else @click="unable${table.entityName}( scope.row)">启用
          </el-button>
          <el-button size="mini" type="danger" @click="delete${table.entityName}( scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <el-dialog :title="edit${table.entityName}Title" :visible.sync="dialogVisible" width="50%"
               @close="handleClose('form${table.entityName}')">
      <el-form ref="form${table.entityName}" :model="form" label-width="80px" :rules="rules">
        <#-- ----------  BEGIN 字段循环遍历  ---------->
        <#list table.fields as field>
          <#if !field.keyFlag>
          <#-- 时间序列化 -->
            <el-form-item label="${field.comment}" prop="${field.propertyName}">
              <#if field.type == "datetime">
                <el-date-picker
                        v-model="form.${field.propertyName}"
                        type="datetime"
                        value-format="yyyy-MM-dd HH:mm:ss"
                        placeholder="选择日期时间">
                </el-date-picker>
              <#else>
                <el-input size="small" v-model="form.${field.propertyName}"></el-input>
              </#if>
            </el-form-item>
          </#if>
        </#list>
        <el-form-item>
          <el-button size="small" type="primary" @click="save${table.entityName}('form${table.entityName}')">保存
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  // 导入请求方法
  import {deptTree} from '../../api/userApi'
  import {addDeptCodeNameMap} from '../../utils/util'
  import {
    pageList${table.entityName}, list${table.entityName}, get${table.entityName}, use${table
  .entityName}, save${table.entityName}, delete${table.entityName}} from '../../api/${table.entityName}'
  import Pagination from '../../components/Pagination'

  export default {
    // 注册组件
    components: {
      Pagination
    },
    data() {
      return {
        edit${table.entityName}Title: '',
        dialogVisible: false,
        formInline: {
          page: 1,
          num: 10
        },
        // 分页参数
        pageparm: {
          currentPage: 1,
          pageSize: 10,
          total: 0
        },
        listData: [],
        form: {
          <#list table.fields as field>
          <#if !field.keyFlag>
          <#if field.propertyType == "String">
          ${field.propertyName}: '',
          <#elseif field.type == "datetime">
          ${field.propertyName}: '',
          <#else>
          ${field.propertyName}: 0,
          </#if>
          </#if>
          </#list>
        },
        // rules表单验证
        rules: {
          <#list table.fields as field>
          <#if !field.keyFlag>
          ${field.propertyName}: [
            {required: true, message: '请输入${field.comment}', trigger: 'blur'}
          ],
          </#if>
          </#list>
        }
      }
    },
    /**
     * 创建完毕
     */
    created() {
      this.get${table.entityName}Page();
    },
    /**
     * 里面的方法只有被调用才会执行
     */
    methods: {
      toAdd${table.entityName}(type, row) {
        if ('add' === type) {
          this.edit${table.entityName}Title = '新增${table.comment}';
        } else {
          this.edit${table.entityName}Title = '编辑${table.comment}';
          this.form = JSON.parse(JSON.stringify(row));
        }
        this.dialogVisible = true;
      },
      search${table.entityName}() {
        this.get${table.entityName}Page();
      },
      // 分页插件事件
      callFather(parm) {
        this.formInline.page = parm.currentPage;
        this.formInline.num = parm.pageSize;
        this.get${table.entityName}Page();
      },
      // 获取数据
      get${table.entityName}Page() {
        let that = this;
        that.listData = [];
        pageList${table.entityName}(that.formInline)
                .then(res => {
                  if (res.status == 200) {
                    that.listData = res.data.list;
                    // 分页赋值
                    that.pageparm.currentPage = that.formInline.page;
                    that.pageparm.pageSize = that.formInline.num;
                    that.pageparm.total = res.data.total;
                  }
                })
                .catch(err => {
                  that.$message.error('查询${table.comment}列表失败，请稍后再试！');
                })
      },
      handleClose(formName) {
        this.$refs[formName].clearValidate();
        this.$refs[formName].resetFields();
      },
      // 保存菜单
      save${table.entityName}(editData) {
        let that = this;
        this.$refs[editData].validate(valid => {
          if (valid) {
            save${table.entityName}(this.form)
                    .then(res => {
                      if (res.status == 200) {
                        that.dialogVisible = false;
                        that.get${table.entityName}Page();
                      }
                    })
                    .catch(err => {
                      that.$message.error('${table.comment}保存失败，请稍后再试！');
                    })
          } else {
            return false;
          }
        })
      },
      unable${table.entityName}(row) {
        let param = {};
        let titleStart = '您确定要';
        param.id = row.id;
        if (row.status == 0) {
          param.status = 1;
          titleStart = titleStart + '启';
        } else {
          param.status = 0;
          titleStart = titleStart + '停';
        }
        this.$confirm(titleStart + '用${table.comment}“' + row.title + '”吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          use${table.entityName}(param)
                  .then(res => {
                    this.get${table.entityName}Page();
                  })
                  .catch(err => {
                    this.$message.error('${table.comment}操作失败，请稍后再试！');
                  })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
      },
      // 删除菜单
      delete${table.entityName}(row) {
        this.$confirm('您确定要删除${table.comment}“' + row.title + '”吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delete${table.entityName}(row.id)
                  .then(res => {
                    this.get${table.entityName}Page();
                  })
                  .catch(err => {
                    this.$message.error('${table.comment}删除失败，请稍后再试！');
                  })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        })
      }
    }
  }
</script>

<style>

</style>

